package com.gdit.zax.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.Constant;
import com.gdit.zax.dao.CourseDao;
import com.gdit.zax.dao.VideoDao;
import com.gdit.zax.entitys.CourseSee;
import com.gdit.zax.factory.DAOFactory;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.LogUtil;
import com.gdit.zax.utils.O;
import com.gdit.zax.utils.TextUtil;
import com.gdit.zax.utils.webUtil;

public class GetIndexClass extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		long starttime = System.currentTimeMillis();
		
//		String accesstoken = request.getParameter("accesstoken");
		int type = TextUtil.String2int(request.getParameter("type"), -1);
		int page = TextUtil.String2int(request.getParameter("page"), 1);
		
		//检测参数
//		if (TextUtil.isEmpty("accesstoken")) {
//			O.printFail(response, 1, "请检查accesstoken参数!");
//			return;
//		}
		if (type < 0) {
			O.printFail(response, 1, "请检查type参数!");
			return;
		}
		
		
//		int uid = VeriFicUtil.getVFID(accesstoken);
		Connection conn = DBUtil.getConnection();
		
//		if (uid==Integer.MIN_VALUE) {
//			O.printNoLogin(response);
//			return;
//		}
		
		if (conn==null) {
			O.printFail(response, 1000, "数据库连接失败!");
			return;
		}
		
		DAOFactory dao = DAOFactory.init(conn, request, response);
		VideoDao vdao = dao.getVideoDao();
		CourseDao cdao = dao.getCourseDao();
		ArrayList<CourseSee> list = null;
		if (type==Constant.INDEXCLASS_type_hot) {
			list = vdao.listCoursebyhot(page, 10);
		} else if (type==Constant.INDEXCLASS_type_new) {
			list = vdao.listCoursebynew(page, 10);
		} else if (type==Constant.INDEXCLASS_type_comment) {
			list = vdao.listCoursebyrecomment(page, 10);
//			O.printFail(response, 1, "暂时没有实现!");
		} else {
			O.printFail(response, 1, "获取的课程类型不正确!");
			return;
		}
		

		ArrayList<HashMap<String, Object>> olist = new ArrayList<HashMap<String,Object>>();
		for(CourseSee ca:list) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("imageurl",webUtil.getWebAbsoluteUrl(ca.getT_course_pic(), Constant.PIC_video));
			map.put("id", ca.getT_course_id());
			map.put("time", ca.getT_course_addtime());
			map.put("name", ca.getT_course_name());
			map.put("introduction", ca.getT_course_introduction());
			map.put("anthorname", ca.getT_user_nick());
			map.put("seesum", ca.getT_course_seesum());
			map.put("commentsum", ca.getT_course_commentsum());
			map.put("collectnum", cdao.getCourseCcount(ca.getT_course_id()));
			map.put("coursedynamic", ca.getT_course_dynamic());
			olist.add(map);
		}
		O.printSuccess(response, olist);
	
	
	}

}
